﻿using System;
using System.Buffers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using SuperSocket;
using SuperSocket.Channel;

namespace Lawn.AppServer
{
    public class MessageAsyncCommand:IMessageAsyncCommand
    {


        private IServiceProvider serviceProvider;

        private IMessageFactory factory => serviceProvider.GetRequiredService<IMessageFactory>(); 
      
        public MessageAsyncCommand(IServiceProvider _serviceProvider)
        {
             serviceProvider = _serviceProvider;
            
        }
        public async ValueTask PackageHandler(IAppSession session, MessagePackageInfo package)
        {

            await factory.Creat(package).Handler(session, package);
           
            
            
            /*var ss=  sessionContainer.GetSession(session.SessionID);

           if (ss != null)
           {
               Log.Information($"{await sessionContainer.GetSessionCount()} ss is not null");
           }

            if (package == null)
            {
                await session.CloseAsync(CloseReason.ProtocolError);
            }
            else
            {
                foreach (var asession in await sessionContainer.GetAllSession())
                {


                    byte[] body = new byte[]
                    {
                        0x00, 0x01, 0x00, 0x02, 0x00, 0x01
                    };

                    await asession.SendAsync(body.ToArray());

                    // await asession.SendAsync(Encoding.UTF8.GetBytes("abc 123 \r\n"));
                }
                // await session.SendAsync(Encoding.UTF8.GetBytes("abc 123 \r\n"));
            }*/
               
            await Task.CompletedTask;
        }
    }
}



/*
 *
  await _factory.Creat(package).Handler(session, package);
*/